import {reactive} from "vue";
import Action from "@/modal/Action";
import {ActionName} from "@/components/type";
import Score from "@/modal/Score";
import Goods from "@/modal/Goods";
import {ScoreType} from "@/components/ScorePanel/type";

const score = reactive(Score.getInstance());
const goods = reactive(Goods.getInstance());
const action = reactive(Action.getInstance());
// 蜂巢
export default class Beehive {
  readonly type: string = "beehive";
  protected currentStage_: number = 0;
  readonly ripenStage: number = 9;
  readonly productRipen: number = 4;
  readonly baseRipen: number = 5;
  readonly isBase: boolean = true;

  get currentStep() {return this.currentStage_;}

  private growBase() {
    if (this.currentStep < this.baseRipen) this.currentStage_++;
  }

  growUp(beeType: Beehive) {
    let tempStage: number = 0;
    if (beeType && beeType.isBase) {
      this.growBase();
      this.currentStage_ += tempStage;
    } else {
      if (beeType instanceof Beeswax) tempStage = beeType.growSelf();
      if (beeType instanceof Honey) tempStage = beeType.growSelf();
      if (beeType instanceof Chamber) tempStage = beeType.growSelf();
      this.currentStage_ = this.baseRipen + tempStage;
    }
    return this;
  }

  get isRipen() {return this.currentStep == this.ripenStage;}

  get isAward() {return this.currentStage_ == 2 || this.currentStage_ == 4;}

  awardStep(index: number) {
    return index == 2 || index == 4;
  }

  getAward() {
    if (this.isAward) {
      if (this.currentStage_ === 2) action.setAction([ActionName.FRUIT]);
      else action.setAction([ActionName.FRUIT, ActionName.FRUIT]);
    }
  }
}

// 蜂蜡
export class Beeswax extends Beehive {
  readonly name: string = "beeswax";
  private beeswaxStage_: number = 0;
  readonly isBase: boolean = false;

  get selfStep() {return this.beeswaxStage_}

  get score(): number {
    return this.currentStep == this.ripenStage ? 5 :
      this.isAward ? 2 : 0;
  }

  showScore(steps: number) {
    return steps == this.productRipen ? 5 :
      steps != this.productRipen - 1 ? 2 : 0;
  }

  get goodsNUm(): number {
    if (this.currentStep == 9 || this.currentStep == 7) return 4;
    else if (this.isAward) return 2;
    else return 0;
  }

  showGoods(steps: number) {
    return steps == this.productRipen - 3 ? 2 :
      steps != this.productRipen - 1 ? 4 : 0;
  }

  growSelf() {return this.isRipen ? 4 : ++this.beeswaxStage_;}

  override get isAward() {return this.currentStep === this.ripenStage || this.currentStep > 5 && this.currentStep < 8;}

  awardStep(index: number) {
    return index != 3;
  }

  override getAward() {
    if (this.isAward) {
      score.addScore(this.score, ScoreType.BEEHIVE);
      goods.gainGoods(this.goodsNUm);
    }
  }
}

// 蜂蜜
export class Honey extends Beehive {
  readonly name: string = "honey";
  private honeyStage_: number = 0;
  readonly isBase: boolean = false;

  get selfStep() {return this.honeyStage_}

  get score(): number {
    return this.currentStep == 9 ? 8 :
      this.isAward ? 5 : 0;
  }

  showScore(steps: number) {
    return steps == this.productRipen ? 8 :
      steps != this.productRipen - 1 ? 5 : 0;
  }

  growSelf() {return this.isRipen ? 4 : ++this.honeyStage_;}

  override get isAward() {return this.currentStep === this.ripenStage || this.currentStep > 5 && this.currentStep < 8;}

  awardStep(index: number) {
    return index != 3;
  }

  override getAward() {
    if (this.isAward) {
      score.addScore(this.score, ScoreType.BEEHIVE);
    }
  }
}

// 子巢
export class Chamber extends Beehive {
  readonly name: string = "chamber";
  private chamberStage_: number = 0;
  readonly isBase: boolean = false;

  get selfStep() {return this.chamberStage_}

  get score() {
    return this.currentStep == 6 ? 3 : 0
  }

  showScore(steps: number) {
    return steps === (this.productRipen - 3) ? 3 : 0;
  }

  growSelf() {return this.isRipen ? 4 : ++this.chamberStage_;}

  override get isAward() {return this.currentStep > 5;}

  awardStep(index: number) {
    return true;
  }

  override getAward() {
    if (this.isAward) {
      switch (this.currentStep) {
        case 6:
          score.addScore(this.score, ScoreType.BEEHIVE);
          break;
        case 7:
          action.setAction([ActionName.PLANT]);
          break;
        case 8:
          action.setAction([
            ActionName.FRUIT,
            ActionName.FRUIT,
            ActionName.FRUIT
          ]);
          break;
        case 9:
          action.setAction([
            ActionName.FARM,
            ActionName.FARM
          ]);
          break;
      }
    }
  }
}
